var wemark = require('../../wemark/wemark.js')
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    articleId: null,
    userId: null,
    readNum: 0,
    likeNum: 0,
    isLike: false,
    likeImage: '/image/collect1.png',
    hasUserInfo: false,
    // 确定一个数据名称
    wemark: {},
    commentList: [],

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //options为页面路由过程中传递的参数
    console.log('onLoad');
    var self = this;
    self.setData({
      articleId: options.articleId,
      mername: options.title
    })
    wx.setNavigationBarTitle({
      title: self.data.mername//页面标题为路由参数
    })
    //获取用户ID
    wx.getStorage({
      key: "openId",
      success: function (res) {
        self.setData({
          userId: res.data
        })
      }
    })

    // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
    // 所以此处加入 callback 以防止这种情况
    app.userInfoReadyCallback = res => {
      this.setData({
        hasUserInfo: true
      })
    }
    //全局用户信息
    if (app.globalData.userInfo) {
      this.setData({
        hasUserInfo: true
      });
    }

    this.getArticleDetail()
    this.queryLike()
    this.addReadNum()

  },
  // 函数如何传参？
  getArticleDetail: function () {
    var self = this;
    wx.request({
      url: app.globalData.serveUrl + '/article/detail',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      //请求的参数
      data: {
        id: self.data.articleId
      },
      success: function (res) {
        self.setData({
          readNum: res.data.data.readNum,
          likeNum: res.data.data.likeNum,
        });
        var content = res.data.data.content;
        wemark.parse(content, self, {
          // 新版小程序可自适应宽高
          // imageWidth: wx.getSystemInfoSync().windowWidth - 40,
          name: 'wemark'
        })
      },
      fail: function () {

      }
    })
  },
  //评论列表
  getCommentList: function () {
    var self = this;
    wx.request({
      url: app.globalData.serveUrl + '/comment/list',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        articleId: self.data.articleId
      },
      success: function (res) {
        var code = res.data.code;
        if (code == 0) {
          var datas = res.data.data;
          self.setData({
            commentList: datas,
          });
        } else {

        }

      },
      fail: function () {

      },

    })


  },

  //查询当前用户是否对这篇文章进行了点赞
  queryLike: function () {
    var self = this;
    wx.request({
      url: app.globalData.serveUrl + '/likeNum/query',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        articleId: self.data.articleId,
        userId: self.data.userId,
      },
      success: function (res) {
        var code = res.data.code;
        if (code == 0) {
          //点赞过
          self.setData({
            isLike: true,
            likeImage: '/image/collect1.png',
          });
        } else {
          //没有点赞过
          self.setData({
            isLike: false,
            likeImage: '/image/collect0.png',
          });
        }
      },
      fail: function () {

      },

    })


  },

  //点赞点击事件
  addLikeClick: function () {
    if (this.data.isLike) {
      this.cancelLikeNum();
    } else {
      this.addLikeNum();
    }
  },

  //调用点赞接口
  addLikeNum: function () {
    var self = this;
    wx.request({
      url: app.globalData.serveUrl + '/likeNum/add',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        articleId: self.data.articleId,
        userId: self.data.userId,
      },
      success: function (res) {
        var code = res.data.code;
        if (code == 0) {
          self.setData({
            isLike: true,
            likeImage: '/image/collect1.png',
            likeNum: self.data.likeNum + 1
          });
          wx.showToast({
            icon: 'success',
            duration: 2000
          })
        } else {

        }

      },
      fail: function () {

      },
    })
  },

  //调用取消点赞接口
  cancelLikeNum: function () {
    var self = this;
    wx.request({
      url: app.globalData.serveUrl + '/likeNum/cancel',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        articleId: self.data.articleId,
        userId: self.data.userId,
      },
      success: function (res) {
        var code = res.data.code;
        if (code == 0) {
          self.setData({
            isLike: false,
            likeImage: '/image/collect0.png',
            likeNum: self.data.likeNum - 1
          });
          wx.showToast({
            icon: 'success',
            duration: 2000
          })
        } else {

        }

      },
      fail: function () {

      },

    })


  },

  //增加阅读量接口
  addReadNum: function () {
    var self = this;
    wx.request({
      url: app.globalData.serveUrl + '/readNum/add',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        articleId: self.data.articleId,
        userId: self.data.userId,
      },
      success: function (res) {
        var code = res.data.code;
        if (code == 0) {
          self.setData({
            readNum: self.data.readNum + 1
          });
        } else {

        }

      },
      fail: function () {

      },

    })


  },

  //授权获取用户信息，如果没有登录，其实这里「留言」是一个按钮
  getUserInfo: function (e) {
    console.log('getUserInfo');
    var self = this
    // //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      self.setData({
        hasUserInfo: true
      })
      self.addComment();
    })
  },

  //跳转到留言页面
  addComment: function () {
    console.log('addComment');
    var self = this;
    //跳转页面
    wx.navigateTo({
      url: '/pages/add-comment/add-comment?articleId=' +
      self.data.articleId + '&title=' + self.data.mername,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log('onReady');
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('onShow');
    this.getCommentList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('onPullDownRefresh');
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('onReachBottom');
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})